{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MNIST Digit Prediction with k-Nearest Neighbors\n",
    "\n",
    "This script will load the MNIST data, and split it into test/train and perform prediction with nearest neighbors.\n",
    "\n",
    "For each test integer, we will return the closest image/integer.\n",
    "\n",
    "Integer images are represented as 28x8 matrices of floating point numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "from tensorflow.python.framework import ops\n",
    "ops.reset_default_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sess = tf.Session()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate random sample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.seed(13)  # set seed for reproducibility\n",
    "train_size = 1000\n",
    "test_size = 102\n",
    "rand_train_indices = np.random.choice(len(mnist.train.images), train_size, replace=False)\n",
    "rand_test_indices = np.random.choice(len(mnist.test.images), test_size, replace=False)\n",
    "x_vals_train = mnist.train.images[rand_train_indices]\n",
    "x_vals_test = mnist.test.images[rand_test_indices]\n",
    "y_vals_train = mnist.train.labels[rand_train_indices]\n",
    "y_vals_test = mnist.test.labels[rand_test_indices]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Declare k-value and batch size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "k = 4\n",
    "batch_size=6\n",
    "\n",
    "# Placeholders\n",
    "x_data_train = tf.placeholder(shape=[None, 784], dtype=tf.float32)\n",
    "x_data_test = tf.placeholder(shape=[None, 784], dtype=tf.float32)\n",
    "y_target_train = tf.placeholder(shape=[None, 10], dtype=tf.float32)\n",
    "y_target_test = tf.placeholder(shape=[None, 10], dtype=tf.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Declare distance metric\n",
    "#### L1 metric - uncomment following line and comment L2 metric below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "distance = tf.reduce_sum(tf.abs(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), axis=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### L2 metric - uncomment following line and comment the L1 metric above"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), reduction_indices=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Predict: Get min distance index (Nearest neighbor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy on test set: 0.8823529411764696\n"
     ]
    }
   ],
   "source": [
    "# Get min distance index (Nearest neighbor)\n",
    "top_k_xvals, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)\n",
    "prediction_indices = tf.gather(y_target_train, top_k_indices)\n",
    "\n",
    "# Predict the mode category\n",
    "count_of_predictions = tf.reduce_sum(prediction_indices, axis=1)\n",
    "prediction = tf.argmax(count_of_predictions, axis=1)\n",
    "\n",
    "# Calculate how many loops over training data\n",
    "num_loops = int(np.ceil(len(x_vals_test)/batch_size))\n",
    "\n",
    "test_output = []\n",
    "actual_vals = []\n",
    "for i in range(num_loops):\n",
    "    min_index = i*batch_size\n",
    "    max_index = min((i+1)*batch_size,len(x_vals_train))\n",
    "    x_batch = x_vals_test[min_index:max_index]\n",
    "    y_batch = y_vals_test[min_index:max_index]\n",
    "    predictions = sess.run(prediction, feed_dict={x_data_train: x_vals_train, x_data_test: x_batch,\n",
    "                                         y_target_train: y_vals_train, y_target_test: y_batch})\n",
    "    test_output.extend(predictions)\n",
    "    actual_vals.extend(np.argmax(y_batch, axis=1))\n",
    "\n",
    "accuracy = sum([1./test_size for i in range(test_size) if test_output[i]==actual_vals[i]])\n",
    "print('Accuracy on test set: ' + str(accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWQAAAD4CAYAAADbyJysAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHF1JREFUeJzt3XuUFMW9B/DvT0BACCgiKIpgiAE8oBBXQrxqEBZUrgeI\nqwc2ohch3OsLETSKiCc8VIhy4MAJ4FVEfAQxuhKRm4jykqMGFIXlcRc0RBDBmIsBBHxFrPvHNGVV\nsTPbM9MzUz37/ZzD4VdbPd0FtfPbnt9Wd4tSCkREVHjHFXoARESUwIRMROQJJmQiIk8wI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ZWWljs0SAQDMmzdPx127drX6Ut1VMR3m3d+a\nNWuWdLuzzz47kuNFgWfIRESeYEImIvIEEzIRkSdqfQ35xhtvtNozZ87UsYh9FevBgwd17C7VIb+U\nlpaG3pZL3XLDfNLzW2+9ZfWZNWX3ye+9e/fW8YIFC6y+qVOnWu3+/fvr2K0F33LLLTpu2bJl0nH6\n9ARqniETEXmCCZmIyBOxvdtbv379rPbixYuTbtu9e3cdjx8/3urr0aOH1TavKPruu++svuHDh+v4\niSeeCDvUnCimu4LlQjp3e3NLU4VUrPN6+umnW+21a9fquFWrVqH3474nzbnLdB7d8taKFSsy2k8N\neLc3IqI4YUImIvIEEzIRkSditezNXJLm3s3LXIbm1qvMB5LWr18/9PEeeOABq13oujFRXO3evdtq\nm0vixowZY/UNHjxYx+5Te9wHD4f18ccfW+1p06bpeNWqVRntMxd4hkxE5AkmZCIiT8Rq2dv+/ft1\n3KRJk0j2aS6/Aey7vZkPLgWOvSl9IRXr8qhcSTV3XPbmF7OcMWrUKKvvggsusNrmXdyWLl1q9R04\ncCDpfsyrbvOEy96IiOKECZmIyBNMyEREnohVDbmsrEzHCxcutPrq1Kmj4/Xr11t95gMV3brwrFmz\nrPahQ4eyHmc+sNaYHnPe27dvb/Wxhkx5wBoyEVGcMCETEXkiVlfqVVRU6LhevXoFHAnFjXkHL7dk\nYT4QlTerp0LiGTIRkSeYkImIPMGETETkiVgte6PvcXlUceK8Fi0ueyMiihMmZCIiTzAhExF5ggmZ\niMgTTMhERJ5gQiYi8kS6l07vBbAzFwOhtLSJeH+cVz9wXotXqLlNax0yERHlDksWRESeYEImIvIE\nEzIRkSeYkImIPMGETETkCSZkIiJPMCETEXmCCZmIyBNMyEREnvh/7/I8Rp0evG4AAAAASUVORK5C\nYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1036910b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "# Plot the last batch results:\n",
    "actuals = np.argmax(y_batch, axis=1)\n",
    "\n",
    "Nrows = 2\n",
    "Ncols = 3\n",
    "for i in range(len(actuals)):\n",
    "    plt.subplot(Nrows, Ncols, i+1)\n",
    "    plt.imshow(np.reshape(x_batch[i], [28,28]), cmap='Greys_r')\n",
    "    plt.title('Actual: ' + str(actuals[i]) + ' Pred: ' + str(predictions[i]),\n",
    "                               fontsize=10)\n",
    "    frame = plt.gca()\n",
    "    frame.axes.get_xaxis().set_visible(False)\n",
    "    frame.axes.get_yaxis().set_visible(False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf-cpu]",
   "language": "python",
   "name": "conda-env-tf-cpu-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
